#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>


static struct class *first_class;
static struct class_device *first_class_dev;

static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;

static int first_drv_open(struct inode *inode, struct file *file)
{
	*gpfcon &= ~((3<<8) | (3<<10) | (3<<12));
	*gpfcon |= ((1<<8) | (1<<10) | (1<<12));
	return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	int val;

	copy_from_user(&val, buf, count);

	if(val == 1)
	{
		*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
	}
	else if(val == 0)
	{
		*gpfdat |= ((1<<4) | (1<<5) | (1<<6));
	}
	
	return 0;
}

static struct file_operations first_drv_fops = {
	.owner = THIS_MODULE,
	.open  = first_drv_open,
	.write = first_drv_write,
};

int major;
static int first_drv_init(void)
{
	major = register_chrdev(0, "first_drv", &first_drv_fops);

	first_class = class_create(THIS_MODULE, "first_drv");

	first_class_dev = class_device_create(first_class, NULL, MKDEV(major, 0), NULL, "first_drv");

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;
	return 0;
}

static void first_drv_exit(void)
{
	unregister_chrdev(major, "first_drv");
	class_device_unregister(first_class_dev);
	class_destroy(first_class);
	iounmap(gpfcon);
}

module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");


